`
https://leetcode.cn/problems/maximum-width-of-binary-tree/
`

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var widthOfBinaryTree = function (root) {
  if (root === null) return 0;
  let res = 0;
  const q = [{ node: root, id: 1n }]; // 使用BigInt初始化根节点id

  while (q.length) {
    const sz = q.length;
    let start, end;
    for (let i = 0; i < sz; i++) {
      const cur = q.shift();
      if (i === 0) start = cur.id; // 当前层第一个节点的id
      if (i === sz - 1) end = cur.id; // 当前层最后一个节点的id
      if (cur.node.left) q.push({ node: cur.node.left, id: cur.id * 2n });
      if (cur.node.right) q.push({ node: cur.node.right, id: cur.id * 2n + 1n });
    }
    const width = end - start + 1n; // 计算当前层的宽度
    res = Math.max(res, Number(width)); // 转换为Number进行比较，即使树非常深，节点编号也不会溢出
  }

  return res;
};